<?php
class Product_Model extends CI_Model {

    /**
     * construct function
     */
    public  function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    /**
     * Get list product
     * @param <int> $limit
     * @param <int> $offset
     * @param <int> $total
     * @param <int> $categoryId
     */
    public function getListProduct($limit=20, $offset=0, &$total=1, $categoryId=NULL)
    {
        //call query
        $query = "SELECT SQL_CALC_FOUND_ROWS * FROM (products) ";

        if($categoryId)
        {
            $query .= "WHERE CategoryID = {$categoryId} ";
        }

        $query .= "ORDER BY ProductID DESC LIMIT {$offset}, {$limit}";

        //Get result
        $result = $this->db->query($query)->result_array();

        //Get total
        $total = $this->db->query("SELECT FOUND_ROWS() AS total")->row()->total;

        //Return data
        return $result;
    }

    /**
     * Get product detail by productId
     * @param <int> $productId
     * @param <array>
     */
    public function getProductDetail($productId)
    {
        //call query
        $query = $this->db->select('*')->from('products')->where('productID', $productId)->get();

        //Return data
        return $query->row_array();
    }

    /**
     * Insert new product
     * @param <array> $params
     * @return <boolean>
     */
    public function insertProduct($params)
    {
        //Insert to DB
        $result = $this->db->insert('products', $params);

        //Return
        return $result;
    }

    /**
     * Edit product by productId
     * @param <int> $productId
     * @param <array> $params
     * @return <boolean>
     */
    public function editproduct($productId, $params)
    {
        //Insert to DB
        $result = $this->db->where('productID', $productId)->update('products', $params);

        //Return
        return $result;
    }

    /**
     * Delete product
     * @param <int> $productId
     * @return <boolean>
     */
    public function deleteproduct($productId)
    {
        //Delete
        $result = $this->db->where('productID', $productId)->delete('products');

        //Return
        return $result;
    }
    
}
